<?php

/**
 * password_answer actions.
 *
 * @package    myarticles
 * @subpackage password_answer
 * @author     Apul Gupta<apulmca2k4@gmail.com>
 */
class password_answerActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $criteria = new Criteria();
    $criteria->add(PasswordAnswerPeer::USER_ID, $this->getUser()->getAttribute('user_id'));
    $criteria->add(PasswordAnswerPeer::IS_ACTIVE, 1);
    $this->PasswordAnswers = PasswordAnswerPeer::doSelect($criteria);
  }

  public function executeNew(sfWebRequest $request)
  {

    $this->form1 = new PasswordAnswerForm(array(),array('isFront' => true, 'name' => 'first', 'isFirst' => true, 'user_id'=>$this->getUser()->getAttribute('user_id')));

    $this->form2 = new PasswordAnswerForm(array(),array('isFront' => true, 'name' => 'second', 'isFirst' => true, 'user_id'=>$this->getUser()->getAttribute('user_id')));
    
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

     $this->form1 = new PasswordAnswerForm(array(),array('isFront' => true, 'name' => 'first', 'isFirst' => true, 'user_id'=>$this->getUser()->getAttribute('user_id')));

     $this->form2 = new PasswordAnswerForm(array(),array('isFront' => true, 'name' => 'second', 'isFirst' => true, 'user_id'=>$this->getUser()->getAttribute('user_id')));
 
    $this->processForm($request, $this->form1, $this->form2);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($PasswordAnswer = PasswordAnswerPeer::retrieveByPkAndUser($request->getParameter('id'), $this->getUser()->getAttribute('user_id')), sprintf('Object PasswordAnswer does not exist (%s).', $request->getParameter('id')));
    $this->form = new PasswordAnswerForm($PasswordAnswer, array('isFront' => true, 'isFirst' => true, 'user_id'=>$this->getUser()->getAttribute('user_id')));
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($PasswordAnswer = PasswordAnswerPeer::retrieveByPk($request->getParameter('id')), sprintf('Object PasswordAnswer does not exist (%s).', $request->getParameter('id')));
    $this->form = new PasswordAnswerForm($PasswordAnswer, array('isFront' => true, 'isFirst' => true, 'user_id'=>$this->getUser()->getAttribute('user_id')));

    $this->processEditForm($request, $this->form);

    $this->setTemplate('edit');
  }

  

  protected function processForm(sfWebRequest $request, sfForm $form1, sfForm $form2)
  {
      $i = 0;
    $form1->bind($request->getParameter($form1->getName()), $request->getFiles($form1->getName()));
    if ($form1->isValid())
    {
      $PasswordAnswer1 = $form1->save();
      $i++;
      
    }
    $form2->bind($request->getParameter($form2->getName()), $request->getFiles($form2->getName()));
    if ($form2->isValid())
    {
      $PasswordAnswer2 = $form2->save();
      $i++;
      
    }

    if($i==2)
    {
        $this->redirect('password_answer/index');
    }
  }
  protected function processEditForm(sfWebRequest $request, sfForm $form)
  {
     $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $PasswordAnswer = $form->save();
       $this->redirect('password_answer/index');
    }
    
       
    
  }
}
